package com.puhui.education.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.puhui.education.R;

/**
 * Created by Administrator on 2016/6/3.
 */
public class MessageView extends View {
    private Bitmap bitmap; //图片Bitmap对象
    private Paint paint; //画笔
    private RectF imgRectF; //图片所占用的矩形
    private Rect messageTextRect; //消息文本所占用的矩形
    private String messageText; //右侧消息数字
    private int messageTextColor; //消息数字文本颜色
    private int messageBackGroundColor;
    private String bottomText;
    private Rect bottomTextRect; //底部文本所占用的矩形
    private int bottomTextColor;
    private boolean isDrawMessage; //是否绘制右侧消息文字
    private boolean isDrawCircle; //是否绘制右侧消息圆圈
    private float imgSizeScale; //图片缩放比例
    private float bottomDividerHeightScale; //底部分割线缩放比例
    private float middleDividerHeightScale; //底部分割线缩放比例
    private float bottomTextSizeScale; //底部文本字体大小比例
    private float circleRadiusScale; //圆的半径比例
    private String messageTextTemp;

    public MessageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
        initData();

    }

    private void initData() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        bottomTextRect = new Rect();
        messageTextRect = new Rect();

    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MessageView);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.MessageView_middleImg:
                    bitmap = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, R.mipmap.ic_launcher));
                    break;
                case R.styleable.MessageView_imgSizeScale:
                    imgSizeScale = a.getFloat(attr, 0.8f);
                    break;
                case R.styleable.MessageView_isDrawMessage:
                    isDrawMessage = a.getBoolean(attr, false);
                    break;
                case R.styleable.MessageView_isDrawCircle:
                    isDrawCircle = a.getBoolean(attr, false);
                    break;
                case R.styleable.MessageView_rightMessageTextColor:
                    messageTextColor = a.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.MessageView_rightCircleBackGroundColor:
                    messageBackGroundColor = a.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.MessageView_rightMessageText:
                    messageText = a.getString(attr);
                    messageTextTemp = messageText;
                    break;
                case R.styleable.MessageView_bottomText:
                    bottomText = a.getString(attr);
                    break;
                case R.styleable.MessageView_bottomTextSizeScale:
                    bottomTextSizeScale = a.getFloat(attr, 0.22f);
                    break;
                case R.styleable.MessageView_bottomTextColor:
                    bottomTextColor = a.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.MessageView_bottomDividerHeightScale:
                    bottomDividerHeightScale = a.getFloat(attr, 0.11f);
                    break;
                case R.styleable.MessageView_middleDividerHeightScale:
                    middleDividerHeightScale = a.getFloat(attr, 0.11f);
                    break;
                case R.styleable.MessageView_circleRadiusScale:
                    circleRadiusScale = a.getFloat(attr, 1f);
                    break;
            }
        }
        a.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float parentWidth = (float) getWidth();
        float parentHeight = (float) getHeight();
        //绘制最底部文本
        paint.setTextSize(parentHeight * bottomTextSizeScale);
        paint.setColor(bottomTextColor);
        paint.getTextBounds(bottomText, 0, bottomText.length(), bottomTextRect);
        canvas.drawText(bottomText, parentWidth / 2.0f - (float) (bottomTextRect.width()) / 2.0f, parentHeight * (1.0f - bottomDividerHeightScale), paint);
        //绘制中部图片
        float imgSize = parentHeight * imgSizeScale - (float) bottomTextRect.height() - parentHeight * bottomDividerHeightScale; //图片长和宽,这里只适用于宽度比高度大的布局
        float imgRectFLeft = (parentWidth - imgSize) / 2.0f;
        float imgRectFRight = imgRectFLeft + imgSize;
        float imgRectFBottom = parentHeight - (parentHeight * middleDividerHeightScale) - (float) bottomTextRect.height();
        float imgRectFTop = imgRectFBottom - imgSize;
        imgRectF = new RectF(imgRectFLeft, imgRectFTop, imgRectFRight, imgRectFBottom);
        canvas.drawBitmap(bitmap, null, imgRectF, paint);
        //绘制右上角圆形
        float radius = imgRectFTop * circleRadiusScale;
        float circleX = imgRectFRight;
        float circleY = imgRectFTop;
        if (isDrawCircle) {
            paint.setColor(messageBackGroundColor);
            canvas.drawCircle(circleX, circleY, radius, paint);
        }
        //绘制右上角圆形文本
        if (isDrawMessage) {
            if (Integer.valueOf(messageTextTemp) > 99) {
                messageText = "99+";
            }
            paint.setColor(messageTextColor);
            paint.setTextSize(radius*1.05f);
            paint.getTextBounds(messageText, 0, messageText.length(), messageTextRect);
            //这里画右上角消息数字文本存在误差,-radius*0.0725f进行调整
            canvas.drawText(messageText, circleX - (float) messageTextRect.width() / 2.0f-radius*0.0725f, circleY + (float) messageTextRect.height() / 2.0f, paint);
        }
    }

    //设置不绘制消息
    public void setEmptyMessage() {
        isDrawCircle = false;
        isDrawMessage = false;
        invalidate();
    }

    //只绘制圆
    public void drawCircle() {
        isDrawCircle = true;
        isDrawMessage = false;
        invalidate();
    }

    //绘制圆和文字
    public void drawMessageText(String text) {
        messageText = text;
        isDrawCircle = true;
        isDrawMessage = true;
        invalidate();
    }
}
